package com.hctforgreen.greenservice.utils;


import com.hctforgreen.greenservice.DownloadManagerActivityV2;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.DataVersionCodeEntity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 2016-11-5 检查基础数据包版本
 * 
 * @author 390694
 * 
 */
public class CheckDataVersionCodeUtil {
	/**
	 * 1:是最新版本；0：不是最新版本
	 */
	public int isNewVersion = 0;
	

	
	/**
	 * 判断资料版本,并更新
	 * @author 390694
	 * 
	 */
	public class CheckDataVersionCodeTask extends
			AsyncTask<Activity, Void, Integer> {
		DataVersionCodeEntity entity;
		private Context activity2;
		private int tipsId;
		private int tipsTitle;
		private boolean bTopRefresh;
		private LinearLayout lyt_default_content;
		private LinearLayout loadLyt;
		private LinearLayout reloadLyt;
		private Button reloadBtn;
		private TextView loadTv;

		public CheckDataVersionCodeTask(int tipsId, int tipsTitle, Context activity2,
				boolean bTopRefresh, LinearLayout lyt_default_content ,LinearLayout loadLyt,
				LinearLayout reloadLyt, Button reloadBtn, TextView loadTv) {
			this.activity2 = activity2;
			this.bTopRefresh = bTopRefresh;
			this.tipsId = tipsId;
			this.tipsTitle = tipsTitle;
			this.lyt_default_content = lyt_default_content;
			this.loadLyt = loadLyt;
			this.reloadLyt = reloadLyt;
			this.reloadBtn = reloadBtn;
			this.loadTv = loadTv;

		}

		@Override
		protected Integer doInBackground(Activity... activity) {
			try {
				HctController hctController = new HctController(activity[0]);

				HctResult result = hctController.getBookDataVersionCode();

				if (result.status == HctConstants.STATUS_SUCCESS) {
					entity = (DataVersionCodeEntity) result.data;
					String localVersionCode = DataVersionCodeUtil
							.getBookVersion(activity[0]);
					if (localVersionCode.equals(entity.versionCode)) {

						isNewVersion = 1;

					} else {
						isNewVersion = 0;
					}

				} else {

					isNewVersion = 1;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return isNewVersion;
		}

		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result == 0) {
				//lyt_default_content.setVisibility(View.VISIBLE);
				dialogTips(tipsId, tipsTitle,activity2, true, loadLyt,
						reloadLyt, reloadBtn, loadTv,entity);
			}else{
				//lyt_default_content.setVisibility(View.GONE);
			}
		}

	}

	/**
	 * 对话框提示 390694
	 * 
	 * @param tipsId
	 */
	public void dialogTips(int tipsId,int tipsTitle, final Context activity,
			final boolean bTopRefresh, final LinearLayout loadLyt,
			final LinearLayout reloadLyt, final Button reloadBtn,
			final TextView loadTv,final DataVersionCodeEntity entity) {
		try {
			Dialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
			builder.setTitle(tipsTitle)
					.setMessage(tipsId)
					.setPositiveButton(R.string.terminal_update_data_yes,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();

									//2016-12-23 390694
									//跳转到下载管理去刷新
									Intent intent = new Intent(activity, DownloadManagerActivityV2.class);
									intent.putExtra("fromTerminalLearning",true);
									activity.startActivity(intent);

									/*getAllBooksWithAllChildSeriesAndDbSerieName(
											activity, bTopRefresh, loadLyt,
											reloadLyt, reloadBtn, loadTv,entity);*/

								}
							})
					.setNegativeButton(R.string.terminal_update_data_no,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();

								}
							});
			dialog = builder.create();
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取资料下面的子系列
	 * 
	 * 390694---20160325添加
	 */
	private void getAllBooksWithAllChildSeriesAndDbSerieName(
			final Activity activity, final boolean bTopRefresh,
			final LinearLayout loadLyt, final LinearLayout reloadLyt,
			final Button reloadBtn, final TextView loadTv,final DataVersionCodeEntity entity) {

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					//Log.i("hh", "加载成功");
					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.GONE);
					//Log.i("hh", "隐藏进度条");
					DataVersionCodeUtil.saveBookVersion(activity,
							entity.versionCode);
					//Log.i("hh", "加载成功 保存版本号   " + entity.versionCode);
					break;

				case HctConstants.STATUS_FAILD:
					//Log.i("hh", "加载失败");
					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.VISIBLE);
					reloadBtn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							getAllBooksWithAllChildSeriesAndDbSerieName(
									activity, bTopRefresh, loadLyt, reloadLyt,
									reloadBtn, loadTv,entity);
						}
					});
					break;
				}
			}

		};
		loadTv.setVisibility(View.VISIBLE);
		loadLyt.setVisibility(View.VISIBLE);
		reloadLyt.setVisibility(View.GONE);
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(activity);

					HctResult result = controller
							.getAllBooksWithAllChildSeries2Db(bTopRefresh, 0);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}



}
